﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GameCoder.BLL;
using GameCoder.Model;

namespace GameCoder.CoderUI
{
    public partial class Form1 : Form
    {
        private GameItem currGame;
        public Form1()
        {
            InitializeComponent();
            //MessageBox.Show(GameDAL.GetCount().ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindGame();
        }

        private void BindGame()
        {
            this.cbGames.DataSource = new GameBLL().GetGames();
            this.cbGames.DisplayMember = "Name";
            if (this.cbGames.SelectedItem != null)
            {
                currGame = this.cbGames.SelectedItem as GameItem;
            }
        }

        private void BindGroup()
        {
            this.cbGroups.DataSource = null;
            if (null != currGame)
            {
                this.cbGroups.DataSource = new GameBLL().GetGroups(currGame.ID);
                this.cbGroups.DisplayMember = "Name";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string name = txtGameName.Text;
            if (!string.IsNullOrEmpty(name))
            {
                currGame = new GameBLL().Add(name);
            }
            BindGame();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string name = txtGroupName.Text;
            if (!string.IsNullOrEmpty(name) && currGame != null)
            {
                new GroupBLL().Add(currGame.ID, name);
            }
            BindGroup();
        }

        private void cbGames_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbGames.SelectedItem != null)
            {
                currGame = this.cbGames.SelectedItem as GameItem;
            }
            BindGroup();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //new GameBLL().Add("XXX");
            //BindGame();

            foreach (var item in new GameBLL().Test())
            {
                listBox1.Items.Add(item.Name + " " + item.HasGroup);
            }
        }

        private void btnDeletGame_Click(object sender, EventArgs e)
        {
            new GameBLL().Delete(currGame.ID);
            BindGame();
        }

        private void btnDeletGroup_Click(object sender, EventArgs e)
        {
            if (cbGroups.SelectedItem != null)
            {
                GroupItem group = cbGroups.SelectedItem as GroupItem;
                new GroupBLL().Delete(group.ID);
                BindGroup();
            }
        }
    }
}
